﻿using NAudio.Lame;
using NAudio.Wave;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DLAudioChat
{
    public static class AudioConverter
    {
        public static MemoryStream ConvertPcmToMp3(MemoryStream pcmStream, int bitRate = 128)
        {
            var mp3Stream = new MemoryStream();
            using (var writer = new LameMP3FileWriter(mp3Stream, new WaveFormat(16000, 16, 1), bitRate))
            {
                pcmStream.Position = 0;
                using (var reader = new RawSourceWaveStream(pcmStream, new WaveFormat()))
                {
                    byte[] buffer = new byte[reader.WaveFormat.AverageBytesPerSecond];
                    int bytesRead;
                    while ((bytesRead = reader.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        writer.Write(buffer, 0, bytesRead);
                    }
                }
            }
            return mp3Stream;
        }
        //public static MemoryStream ConvertMp3ToPcm(MemoryStream mp3Stream)
        //{
        //    var pcmStream = new MemoryStream();
        //    using (var mp3File = new Mp3FileReader(mp3Stream))
        //    {
        //        using (var conversionStream = WaveFormatConversionStream.CreatePcmStream(mp3File))
        //        {
        //            WaveFileWriter.WriteWavFileToStream(pcmStream, conversionStream);
        //        }
        //    }
        //    return pcmStream;
        //}
        public static MemoryStream ConvertHexToPcm(string hexString)
        {
            byte[] mp3Bytes = StringToByteArray(hexString);

            using (MemoryStream mp3Stream = new MemoryStream(mp3Bytes))
            {
                using (Mp3FileReader mp3File = new Mp3FileReader(mp3Stream))
                {
                    WaveFormat targetFormat = new WaveFormat(16000, 16, 1); // 目标 PCM 格式，你可以根据实际需求调整
                    using (WaveFormatConversionStream conversionStream = new WaveFormatConversionStream(targetFormat, mp3File))
                    {
                        MemoryStream pcmStream = new MemoryStream();
                        WaveFileWriter.WriteWavFileToStream(pcmStream, conversionStream); // 将转换后的数据写入pcmStream
                        return pcmStream;
                    }
                }
            }
        }

        public static MemoryStream ConvertMP3ToPCM(string hexString)
        {
            // 将十六进制字符串转换为字节数组
            byte[] mp3Data = StringToByteArray(hexString);
            // 将字节数组转换为内存流
            MemoryStream mp3Stream = new MemoryStream(mp3Data);
            // 将MP3数据流保存到临时文件
            string tempFilePath = Path.GetTempFileName();
            using (FileStream tempFileStream = File.OpenWrite(tempFilePath))
            {
                mp3Stream.CopyTo(tempFileStream);
            }

            MemoryStream pcmStream = new MemoryStream();

            using (Mp3FileReader mp3Reader = new Mp3FileReader(tempFilePath))
            {
                using (WaveStream pcmStream32 = WaveFormatConversionStream.CreatePcmStream(mp3Reader))
                {
                    WaveFileWriter.WriteWavFileToStream(pcmStream, pcmStream32);
                }
            }
            // 删除临时文件
            File.Delete(tempFilePath);
            pcmStream.Position = 0;
            return pcmStream;
        }

        private static byte[] StringToByteArray(string hex)
        {
            int numberChars = hex.Length;
            byte[] bytes = new byte[numberChars / 2];
            for (int i = 0; i < numberChars; i += 2)
            {
                bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
            }
            return bytes;
        }

        public struct MP3_Frame_t
        {
            public byte frame_type;          // 帧类型
            public byte device_id;           // 设备id码
            public byte[] mp3_data;          // MP3数据，长度为512

            // 构造函数
            public MP3_Frame_t(byte type, byte id, byte[] data)
            {
                frame_type = type;
                device_id = id;
                mp3_data = data.Length >= 1024 ? data.Take(1024).ToArray() : data.Concat(new byte[1024 - data.Length]).ToArray();
            }
        }
    }
}

